package ntd.util.frame.task.handle.fm.bo;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import com.modular.iot.bo.IotData;
import lombok.Data;
import ntd.util.frame.task.handle.fm.util.ToolFmData;

import java.util.List;

/**
 * 开放平台 业务数据对象
 */
@Data
public class FmDataRecordBo {

    private String id;

    private String mn;

    private String onLine;

    private String siteName;

    private String cn;

    private String province;

    private String provinceString;

    private String city;

    private String cityString;

    private String county;

    private String countyString;

    private String createTime;

    private String receiveTime;

    private Integer dataState;

    private Integer hasA;

    private Integer hasE;

    private Integer hasO;

    private String ruleId;

    private List<FmDataRecordItemBo> dataRecordItemList;

    public static FmDataRecordBo init() {
        FmDataRecordBo fmDataRecordBo = new FmDataRecordBo();
        fmDataRecordBo.setHasA(0);
        fmDataRecordBo.setHasE(0);
        fmDataRecordBo.setHasO(0);
        fmDataRecordBo.setRuleId("-1");
        fmDataRecordBo.setDataState(0);
        return fmDataRecordBo;
    }

    public static FmDataRecordBo init(String mn, String cn, String ruleId, String currentTimeStr) {
        FmDataRecordBo fmDataRecordBo = init();
        fmDataRecordBo.setMn(mn);
        fmDataRecordBo.setCn(StrUtil.subSuf(cn, -2));
        fmDataRecordBo.setReceiveTime(currentTimeStr);
        fmDataRecordBo.setRuleId(StrUtil.blankToDefault(ruleId, "-1"));
        fmDataRecordBo.setCreateTime(currentTimeStr);
        return fmDataRecordBo;
    }

    /**
     * iot 数据转换为fm数据结构
     * @param iotData
     * @return
     */
    public static FmDataRecordBo transform(IotData iotData) {
        Entity siteInfo = ToolFmData.findSiteInfo(iotData.getMn());
        FmDataRecordBo fmDataRecordBo = FmDataRecordBo.init();
        fmDataRecordBo.setMn(iotData.getMn());
        fmDataRecordBo.setCreateTime(DateUtil.formatLocalDateTime(iotData.getReportTime()));
        fmDataRecordBo.setReceiveTime(DateUtil.formatLocalDateTime(iotData.getReportTime()));
        fmDataRecordBo.setSiteName(siteInfo.getStr("site_name"));
        return fmDataRecordBo;
    }
}
